package com.yanqu.road.dataHelper.action.zhounianqing;

import com.yanqu.road.dataHelper.action.excel.ZhouNianQingRankData;
import com.yanqu.road.dataHelper.action.excel.ZhouNianRankLogic;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class ChildDataETLCaller implements Callable<List<ZhouNianQingRankData>> {

    private List<Integer> eventType;

    private int rankType;

    ServerInfo serverInfo;

    int size;

    public ChildDataETLCaller(List<Integer> eventType, int rankType, ServerInfo serverInfo, int size) {
        this.eventType = eventType;
        this.rankType = rankType;
        this.serverInfo = serverInfo;
        this.size = size;
    }

    @Override
    public List<ZhouNianQingRankData> call() throws Exception {

        GameServer testGameServer = new GameServer(serverInfo);
        ZhouNianRankLogic resource = new ZhouNianRankLogic(testGameServer);
        //List<ZhouNianQingRankData> serverRankList = resource.getServerRankList(serverInfo.getServerId(), eventType, size, rankType);

        List<ZhouNianQingRankData> serverRankList = new ArrayList<>();
        //子嗣冲榜
        //本服    1       9
        //跨服    12       6
        Map<Integer, ActivityInfo> activityInfoMap = new ServerMgr().getActivityInfoBy(1, 9, 12, 6, serverInfo.getServerId(), "2021-02-25 00:00:00", "2022-08-15 00:00:00");
        if(activityInfoMap.size() > 0){
            List<Integer> activityIdList = new ArrayList<>();
            for (int id : activityInfoMap.keySet()) {
                activityIdList.add(id);
            }
            serverRankList = resource.getServerRankListByActivityId(serverInfo.getServerId(), activityIdList, size, rankType);
        }

        if (serverRankList.size() > 0){
            //填充活动数据
            //resource.fillActivityInfo(serverRankList);
            for (ZhouNianQingRankData rankData : serverRankList) {
                if(activityInfoMap.containsKey(rankData.getActivityId())) {
                    rankData.setDataTime(activityInfoMap.get(rankData.getActivityId()).getEndTime());
                }
            }
        }else {
            System.out.println("区服没有子嗣冲榜：" + serverInfo.getServerId());
            return serverRankList;
        }

        //最强子嗣数据填充
        //子嗣总赚钱|最强子嗣天资|最强子嗣性别|最强子嗣职业类型|最强子嗣姓名|最强子嗣头像|最强子嗣赚速|是否是双胞胎
        for (ZhouNianQingRankData rankData : serverRankList) {
            List<String> paramList = new ArrayList<>();
            //2020-07-15 00:00:00      1594742400
            //2020-09-01 00:00:00      1598889600
            //2021-07-16 00:00:00      1626364800
            int beginSecond = 1614178800;
            int endSecond = 1660492800;
            List<MarriageInfo> childSiteList = resource.getUserAllChild(rankData.getUserId(), beginSecond, endSecond);
            //加联姻赚速
            BigInteger partnerSpeed = resource.getPartnerChildEarnSpeed(rankData.getUserId(), beginSecond, endSecond);
            MarriageInfo max = null;
            BigInteger total = new BigInteger("0");
            for (MarriageInfo child : childSiteList) {
                total = total.add(child.getChildEarnSpeed());
                if(max == null){
                    max = child;
                }else if(child.getChildEarnSpeed().compareTo(max.getChildEarnSpeed()) > 0){
                    max = child;
                }
            }
            total = total.add(partnerSpeed);
            paramList.add(total.toString());

            if(max == null){
                System.out.println("最强子嗣空异常： " + rankData.getUserId());
                continue;
            }
            paramList.add(""+ max.getChildTalent());
            paramList.add(""+ max.getChildSex());
            paramList.add(""+ max.getChildOccupation());
            paramList.add(""+ max.getChildName());
            paramList.add(""+ max.getChildHeadIcon());
            paramList.add(max.getChildEarnSpeed().toString());
            paramList.add(""+ (max.isTwins() ? 1 : 0));
            rankData.setOtherValue(StringUtils.listToString(paramList, ZhouNianQingRankData.OTHER_VALUE_SEPARATOR));
        }

        return serverRankList;
    }


}
